package com.ht.dao;

import com.ht.bean.HolidayStudent;
import com.ht.bean.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface HolidayStudentDao {

    @Select({"<script>",
            "select s.stuname,h.* from holidaystudent h,student s",
            "<where>",
            "h.stuId = s.stuId and h.stuId=#{holidayStudent.stuId}",
            "<if test=\"holidayStudent.holidayId!=null and holidayStudent.holidayId!=''\">",
            "and h.holidayId = #{holidayStudent.holidayId}",
            "</if>",
            "<if test=\"student.stuname!=null and student.stuname!=''\">",
            "and stuname like concat('%',#{student.stuname},'%')",
            "</if>",
            "</where>",
            "limit #{startIndex},#{pageSize}",
            "</script>"})
    List<Map> selStuHol(@Param("stuId") String stuId, @Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize, @Param("holidayStudent") HolidayStudent holidayStudent, @Param("student") Student student);

    @Select({"<script>",
            "select count(*) from holidaystudent h,student s",
            "<where>",
            "h.stuId = s.stuId and h.stuId=#{holidayStudent.stuId}",
            "<if test=\"holidayStudent.holidayId!=null and holidayStudent.holidayId!=''\">",
            "and h.holidayId = #{holidayStudent.holidayId}",
            "</if>",
            "<if test=\"student.stuname!=null and student.stuname!=''\">",
            "and stuname like concat('%',#{student.stuname},'%')",
            "</if>",
            "</where>",
            "</script>"})
    int countStuHol(@Param("stuId") String stuId, @Param("holidayStudent") HolidayStudent holidayStudent, @Param("student") Student student);

    @Select({"<script>",
            "select s.stuname,h.* from holidaystudent h,student s",
            "<where>",
            "h.stuId = s.stuId",
            "<if test=\"holidayStudent.holidayId!=null and holidayStudent.holidayId!=''\">",
            "and h.holidayId = #{holidayStudent.holidayId}",
            "</if>",
            "<if test=\"student.stuname!=null and student.stuname!=''\">",
            "and stuname like concat('%',#{student.stuname},'%')",
            "</if>",
            "</where>",
            "limit #{startIndex},#{pageSize}",
            "</script>"})
    List<Map> selectAllStuFillin(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize, @Param("holidayStudent") HolidayStudent holidayStudent, @Param("student") Student student);

    @Select({"<script>",
            "select count(*) from holidaystudent h,student s",
            "<where>",
            "h.stuId = s.stuId",
            "<if test=\"holidayStudent.holidayId!=null and holidayStudent.holidayId!=''\">",
            "and h.holidayId = #{holidayStudent.holidayId}",
            "</if>",
            "<if test=\"student.stuname!=null and student.stuname!=''\">",
            "and stuname like concat('%',#{student.stuname},'%')",
            "</if>",
            "</where>",
            "</script>"})
    int countAllStuFillin(@Param("holidayStudent") HolidayStudent holidayStudent, @Param("student") Student student);


    /*
     *请假
     * @param
     * @return
     **/

    @Insert("insert into holidaystudent values(null,#{stuId},#{holidayDay},NOW(),#{endTime},#{title},#{status},#{remark},#{processInstanceId})")
    @SelectKey(before = false, keyColumn = "holidayId", keyProperty = "holidayId", statement = "select last_insert_id()", resultType = Integer.class)
    boolean addstuApplay(HolidayStudent holidayStudent);

    @Select("select * from holidaystudent where holidayId=#{holidayId}")
    public HolidayStudent findObjById(Integer holidayId);

    //查询我的申请单
    @Select("select * from holidaystudent where stuId=#{stuId}")
    public List<HolidayStudent> selHoliday(String stuId);

    //更改表单状态
    @Update("update holidaystudent set status=#{status} where holidayId=#{holidayId}")
    public boolean updHoliday(HolidayStudent holiday);


    @Select("select * from Student")
    List<Student> studentList();


    /*
     *查询各个审批人
     * @param
     * @return
     **/

    //查询任课老师ID
    @Select("SELECT sd.teacher FROM student AS s INNER JOIN studentclass AS sd ON s.clazz = sd.classId where s.stuid = #{stuId} limit 1")
    String selectByTeacher(@Param("stuId") String stuId);

    //查询班主任
    @Select(" select classteacher from student s, studentclass stu  where s.clazz = stu.classId and  s.stuid = #{stuId} limit 1")
    String selectByClaTea(@Param("stuId") String stuId);

    //查询校董办负责人
    @Select("select empId from emp where empid =(select chairman from dep where parentId = 0)")
    String seleceByPrn();

    //删除某个学生请假信息
    @Delete("delete from holidaystudent where holidayId = #{holidayId}")
    boolean deleteHolidayStudent(Integer holidayId);

    @Select("select h.*,s.stuname,sc.className,s.phone from holidayStudent h,student s,studentclass sc " +
            "where h.stuId = s.stuId and s.clazz = sc.classId and h.holidayId = #{holidayId}")
    List<Map> LookHolidayStudentContent(Integer holidayId);
}
